package com.cskaoyan.wx.service;

import com.cskaoyan.bean.User;
import com.cskaoyan.bean.UserExample;
import com.cskaoyan.bo.AuthloginBo;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.wx.bean.bo.WxRegisterBo;
import com.cskaoyan.wx.bean.bo.WxResetBo;
import com.cskaoyan.wx.bean.vo.WxLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WxAuthServiceImpl implements WxAuthService {

    @Autowired
    UserMapper userMapper;

    @Override
    public int register(WxRegisterBo wxRegisterBo, String mobile, String code) {

        if (code == null) {
            return 100;
        }

        //首先判断mobile与code
        if (wxRegisterBo.getMobile().equals(mobile) && wxRegisterBo.getCode().equals(code)) {

            //检查用户名和手机号码重复问题
            UserExample userExample = new UserExample();
            userExample.createCriteria().andUsernameEqualTo(wxRegisterBo.getUsername()).andMobileEqualTo(wxRegisterBo.getMobile());
            List<User> users = userMapper.selectByExample(userExample);
            if (users != null && users.size() > 0) {
                return 400;
            }

            User user = new User();
            user.setUsername(wxRegisterBo.getUsername());
            user.setPassword(wxRegisterBo.getPassword());
            user.setGender(new Byte("0"));
            user.setNickname("新注册用户");
            user.setMobile(wxRegisterBo.getMobile());
            user.setLastLoginIp("0000");
            user.setAvatar("");
            user.setWeixinOpenid(wxRegisterBo.getWxCode());
            user.setStatus(new Byte("0"));
            user.setDeleted(false);
            user.setLastLoginTime(new Date(System.currentTimeMillis()));
            user.setAddTime(new Date(System.currentTimeMillis()));
            user.setUpdateTime(new Date(System.currentTimeMillis()));

            int insert = userMapper.insert(user);

            if (insert <= 0) {
                return 404;
            }

            return 200;

        }
        return 300;
    }

    @Override
    public int reset(WxResetBo wxResetBo, String mobile, String code) {

        if (code == null) {
            return 100;
        }
        //首先判断mobile与code
        if (wxResetBo.getMobile().equals(mobile) && wxResetBo.getCode().equals(code)) {

            //取出相应的用户
            UserExample userExample = new UserExample();
            userExample.createCriteria().andMobileEqualTo(mobile);
            List<User> users = userMapper.selectByExample(userExample);
            if (users == null || users.size() <= 0) {
                return 400;
            }

            if (users.size() > 1) {
                return 404;
            }
            User user = users.get(0);
            user.setPassword(wxResetBo.getPassword());
            int i = userMapper.updateByPrimaryKey(user);

            if (i <= 0) {
                return 404;
            }
            return 200;
        }

        return 300;
    }

    @Override
    public WxLoginVo.UserInfoBean login(AuthloginBo authloginBo) {
        //取出相应的用户
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(authloginBo.getUsername()).andPasswordEqualTo(authloginBo.getPassword());
        List<User> users = userMapper.selectByExample(userExample);
        //封装对象
        User user = users.get(0);
        WxLoginVo.UserInfoBean userInfoBean = new WxLoginVo.UserInfoBean();
        userInfoBean.setNickName(user.getNickname());
        userInfoBean.setAvatarUrl(user.getAvatar());
        return userInfoBean;
    }
}
